<?php
/**
 * Copyright (c) 2009, Jacek Karczmarczyk
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the author nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * PHP Version 5
 *
 * @package Larchana_View
 * @version 0.1
 * @author Jacek Karczmarczyk <jacek@karczmarczyk.pl>
 */



/**
 * @todo A nie po prostu Zend_Date::format?
 */
class Larchana_View_Helper_FormatDate extends Zend_View_Helper_Abstract
{
	const DATE = 'date';
	const DATE_HUMANIZED = 'dateHumanized';
	const TIME = 'time';
	const TIME_HUMANIZED = 'timeHumanized';

	public function formatDate($date, $mode = self::TIME_HUMANIZED)
	{
		if (!preg_match("/^[0-9]+$/", $date)) $date = strtotime($date);
		if (!$date) return '';

		switch($mode)
		{
			case self::DATE: return date("Y-m-d", $date);
			case self::TIME: return date("Y-m-d H:i", $date);
			case self::TIME_HUMANIZED:
				$day = $this->formatDate($date, self::DATE_HUMANIZED);
				$zd = new Zend_Date(date("Y-m-d H:i:s", $date));
				return $day.', '.$zd->get(Zend_Date::TIME_SHORT);
		}

		$dayBeforeYesterday = date("Y-m-d", strtotime("-2 days"));
		$yesterday = date("Y-m-d", strtotime("-1 day"));
		$today = date("Y-m-d", strtotime(time()));
		$tomorrow = date("Y-m-d", strtotime("+1 day"));
		$dayAfterTomorrow = date("Y-m-d", strtotime("+2 days"));
		$date = date("Y-m-d", $date);

		$zd = new Zend_Date($date);

		if ($date == $dayBeforeYesterday) $date = $this->view->translate('date.dayBeforeYesterday');
		else if ($date == $yesterday) $date = $this->view->translate('date.yesterday');
		else if ($date == $today) $date = $this->view->translate('date.today');
		else if ($date == $tomorrow) $date = $this->view->translate('date.tomorrow');
		else if ($date == $dayAfterTomorrow) $date = $this->view->translate('date.dayAfterTomorrow');
		else $date = $date;

		return $date . ' (' . $zd->get(Zend_Date::WEEKDAY) . ')';
	}
}

